package com.mzj.saas.mservice.eligible.controller;

import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.eligible.service.EligibleService;
import com.mzj.saas.mservice.eligible.vo.*;
import com.mzj.saas.mservice.house.vo.CohabitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 资格申请接口
 */
@Controller
@RequestMapping("/eligible")
public class EligibleController {

    @Autowired
    private EligibleService eligibleService;

    /**
     * 权限状态验证,实现了判断用户是否有权限进行公租房的申请
     *
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/verify")
    @ResponseBody
    public ResultBean<Map<String, Object>> verify(@RequestHeader String accessToken) {
        return eligibleService.verify(accessToken);
    }

    /**
     * 证明材料上传
     *
     * @param accessToken  权限鉴定
     * @param cardNumber  身份证
     * @param uploadType  人员类型：0承租人 1同住人
     * @param type        材料类型：1户籍状况证明材料 2婚姻状况证明材料 3工作证明材料 4收入状况证明材料 5住房状况证明材料 6财产状况证明材料 7优先保障人群证明材料
     * @param file        文件
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public ResultBean<Object> uploadFile(@RequestHeader String accessToken,
                                         @RequestParam(value = "cardNumber") String cardNumber,
                                         @RequestParam(value = "uploadType") Integer uploadType,
                                         @RequestParam(value = "type") Integer type,
                                         @RequestParam(value = "file") MultipartFile file) {
        return eligibleService.uploadFile(accessToken, cardNumber, uploadType, type, file);
    }

    /**
     * 上传单位证件照片,实现了对公租房单位的公租房资格申请
     *
     * @param accessToken 权限鉴定
     * @param file 文件
     * @return
     */
    @PostMapping("/uploadUnitPic")
    @ResponseBody
    public ResultBean<Object> uploadUnitPic(@RequestHeader String accessToken,
                                            @RequestParam(value = "orgUserMobile") String orgUserMobile,
                                            @RequestParam(value = "file") MultipartFile file) {
        return eligibleService.uploadUnitPic(accessToken, orgUserMobile, file);
    }

    /**
     * 新增资格申请
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/saveEligible2")
    @ResponseBody
    public ResultBean<EligibleReturnVO> saveEligible2(@RequestHeader String accessToken, @RequestBody EligibleVO vo) {
        return eligibleService.saveEligible(accessToken, vo);
    }

    /**
     * 修改资格申请
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/updateEligible2")
    @ResponseBody
    public ResultBean<Object> updateEligible(@RequestHeader String accessToken, @RequestBody EligibleVO vo) {
        return eligibleService.updateEligible(accessToken, vo);
    }

    /**
     * 查询资料填写流程
     *
     * @param accessToken
     * @param applyCode
     * @param dataId
     * @return
     */
    @GetMapping("/findApplyFlow")
    @ResponseBody
    public ResultBean<HouseApplyFlowVO> findApplyFlow(@RequestHeader String accessToken,
                                                      @RequestParam(value = "applyCode") String applyCode,
                                                      @RequestParam(value = "dataId") String dataId,
                                                      @RequestParam(value = "type", required = false) Integer type) {
        return eligibleService.findApplyFlow(accessToken, applyCode, dataId, type);
    }

    /**
     * 查最新填写流程
     *
     * @param accessToken
     * @param applyCode
     * @return
     */
    @GetMapping("/findLastApplyFlow")
    @ResponseBody
    public ResultBean<HouseApplyFlowVO> findLastApplyFlow(@RequestHeader String accessToken,
                                                          @RequestParam(value = "applyCode") String applyCode) {
        return eligibleService.findLastApplyFlow(accessToken, applyCode);
    }

    /**
     * 编辑时查最新填写流程
     *
     * @param accessToken
     * @param applyId
     * @return
     */
    @GetMapping("/findLastApplyFlow2")
    @ResponseBody
    public ResultBean<HouseApplyFlowVO> findLastApplyFlow2(@RequestHeader String accessToken,
                                                           @RequestParam(value = "applyId") Long applyId) {
        return eligibleService.findLastApplyFlow2(accessToken, applyId);
    }

    /**
     * 新增同住人
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/saveCohabit2")
    @ResponseBody
    @RepeatSubmit
    public ResultBean<EligibleReturnVO> saveCohabit2(@RequestHeader String accessToken, @RequestBody CohabitVO vo) {
        return eligibleService.saveCohabit(accessToken, vo);
    }

    /**
     * 修改同住人
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/updateCohabit2")
    @ResponseBody
    public ResultBean<Object> updateCohabit2(@RequestHeader String accessToken, @RequestBody CohabitVO vo) {
        return eligibleService.updateCohabit(accessToken, vo);
    }

    /**
     * 删除同住人
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/deleteCohabit")
    @ResponseBody
    public ResultBean<Object> deleteCohabit(@RequestHeader String accessToken, @RequestBody CohabitVO vo) {
        return eligibleService.deleteCohabit(accessToken, vo);
    }

    /**
     * 提交申请按钮
     *
     * @param accessToken
     * @param type
     * @param applyCode
     * @return
     */
    @GetMapping("/submit")
    @ResponseBody
    public ResultBean<Object> submit(@RequestHeader String accessToken,
                                     @RequestParam(value = "type") Integer type,
                                     @RequestParam(value = "applyCode") String applyCode) {
        return eligibleService.submit(accessToken, type, applyCode);
    }

    /**
     * 资格申请列表,实现资格申请列表的查询
     *
     * @param accessToken 权限鉴定
     * @param status      1未审核 2审核中 3已审核 4审核不通过 6已撤销 9已过期 10轮候配租 11待配租 12已完成
     * @param pageNumber 当前页
     * @param pageSize 页数
     * @return
     */
    @GetMapping("/eligibleList")
    @ResponseBody
    public ResultBean<ApiData<EligibleListVO>> eligibleList(@RequestHeader String accessToken,
                                                            @RequestParam(value = "name", required = false) String name,
                                                            @RequestParam(value = "type", required = false) Integer type,
                                                            @RequestParam(value = "applyArea", required = false) String applyArea,
                                                            @RequestParam(value = "status", required = false) Integer status,
                                                            @RequestParam(defaultValue = "0") Integer pageNumber,
                                                            @RequestParam(defaultValue = "5") Integer pageSize) {
        return eligibleService.eligibleList(accessToken, name, type, applyArea, status, pageNumber, pageSize);
    }

    /**
     * 审批流程列表
     *
     * @param accessToken 权限鉴定
     * @param id
     * @param backlogItemId
     * @return
     */
    @GetMapping("/findFlow")
    @ResponseBody
    public ResultBean<FindFlowVO<ApplyAuditVO>> findFlow(@RequestHeader String accessToken,
                                                         @RequestParam(value = "id", required = false) Long id,
                                                         @RequestParam(value = "backlogItemId", required = false) Long backlogItemId,
                                                         @RequestParam(value = "type", required = false) Integer type) {
        return eligibleService.findFlow(accessToken, id, backlogItemId, type);
    }

    /**
     * 撤销资格申请
     *
     * @param id
     * @return
     */
    @GetMapping("/backOut")
    @ResponseBody
    public ResultBean<Object> backOut(@RequestParam(value = "id") Long id) {
        return eligibleService.backOut(id);
    }

    /**
     * 承租人详情
     *
     * @param id
     * @return
     */
    @GetMapping("/findEligible2")
    @ResponseBody
    public ResultBean<EligibleVO> findEligible2(@RequestParam(value = "id") Long id,
                                                @RequestParam(value = "type", required = false) Integer type) {
        return eligibleService.findEligible2(id, type);
    }

    /**
     * 同住人详情
     *
     * @param applyCode
     * @param id
     * @return
     */
    @GetMapping("/findCohabit2")
    @ResponseBody
    public ResultBean<CohabitVO> findCohabit2(@RequestParam(value = "applyCode") String applyCode,
                                              @RequestParam(value = "id") String id,
                                              @RequestParam(value = "type", required = false) Integer type) {
        return eligibleService.findCohabit2(applyCode, id, type);
    }

    /**
     * 申请按钮状态,实现了前端页面的申请类型的展示和查询
     *
     * @param accessToken 权限鉴定
     * @return
     */
    @GetMapping("/applyStatus")
    @ResponseBody
    public ResultBean<Object> applyStatus(@RequestHeader String accessToken) {
        return eligibleService.applyStatus(accessToken);
    }

    /**
     * 是否有同住人
     *
     * @param id
     * @return
     */
    @GetMapping("/isCohabit")
    @ResponseBody
    public ResultBean<Object> isCohabit(@RequestParam(value = "id") Long id) {
        return eligibleService.isCohabit(id);
    }

    /**
     * 查询企业绿色通道状态
     *
     * @param id
     * @return
     * @author shun
     * @date 2021年6月30日
     */
    @GetMapping("/audit/status")
    @ResponseBody
    public ResultBean<Object> getApplyOrgAuditStatus(@RequestParam(value = "id") Long id) {
        eligibleService.getApplyOrgAuditStatus(id);
        return null;
    }

    /**
     * 删除资格申请
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteEligible")
    @ResponseBody
    public ResultBean<Object> deleteEligible(@RequestParam(value = "id") Long id) {
        return eligibleService.deleteEligible(id);
    }

    /**
     * 编辑时验证重复申请
     *
     * @param id
     * @return
     */
    @GetMapping("/verifyRepeatApply")
    @ResponseBody
    public ResultBean<Object> verifyRepeatApply(@RequestParam(value = "id") Long id) {
        return eligibleService.verifyRepeatApply(id);
    }


}
